<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <style>
        body {
            background-image: url('/picture/login.jpg');
            background-size: cover;
            background-repeat: no-repeat;
            background-position: center;
            background-attachment: fixed;
            margin: 0;
            padding: 0;
        }
        .container {
            width: 300px;
            margin: 100px auto;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
            background-color: rgba(255, 255, 255, 0.9); /* 半透明白色背景 */
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group input {
            width: 100%;
            padding: 8px;
            box-sizing: border-box;
        }
        .btn {
            width: 100%;
            padding: 10px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>用户登录</h2>
        <div class="form-group">
            <input type="text" id="username" placeholder="用户名">
        </div>
        <div class="form-group">
            <input type="password" id="password" placeholder="密码">
        </div>
        <button class="btn" onclick="login()">登录</button>
        <p>还没有账号？<a href="register.html">立即注册</a></p>
    </div>
    
    <script>
        function login() {
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            
            console.log('登录信息：', {
                username: username,
                password: password
            });
            
            fetch('/api/auth/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    username: username,
                    password: password
                })
            })
            .then(response => {
                if (!response.ok) {
                    throw new Error('Network response was not ok');
                }
                return response.json();
            })
            .then(data => {
                console.log('服务器响应：', data);
                console.log('isAdmin value:', data.isAdmin);
                console.log('isAdmin type:', typeof data.isAdmin);
                
                if (data.success) {
                    sessionStorage.setItem('userId', data.userId);
                    sessionStorage.setItem('username', data.username);
                    sessionStorage.setItem('major', data.major);
                    sessionStorage.setItem('className', data.className);
                    sessionStorage.setItem('isAdmin', data.isAdmin);
                    
                    console.log('Stored isAdmin:', sessionStorage.getItem('isAdmin'));
                    
                    if (parseInt(data.isAdmin) === 1) {
                        console.log('Redirecting to admin page');
                        window.location.replace('admin.html');
                    } else {
                        console.log('Redirecting to index page');
                        window.location.replace('index.html');
                    }
                } else {
                    alert('登录失败：' + (data.message || '未知错误'));
                }
            })
            .catch(error => {
                console.error('请求错误：', error);
                alert('账号或密码错误');
            });
        }
    </script>
</body>
</html> 